/*
 * Copyright (C) 2015 Bilibili
 * Copyright (C) 2015 Zhang Rui <bbcallen@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.detrum.myijkplayer.activities;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.hotspot2.pps.HomeSp;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.ContextMenu;
import android.view.View;
import android.widget.ImageView;

import com.detrum.base.BaseStatic;
import com.detrum.bean.FileBean;
import com.detrum.myijkplayer.widget.media.MyController;
import com.detrum.myijkplayer.widget.media.MyVideoView;
import com.detrum.sportcamera.R;
import com.detrum.utils.LogUtils;
import com.detrum.utils.NfsUtil;
import com.google.gson.Gson;

import tv.danmaku.ijk.media.player.IMediaPlayer;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

public class VideoActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String TAG = "VideoActivity";

    private String mVideoPath;

    private MyVideoView myVideoView;

    private ImageView video_iv;
    private MyController myController;

    private boolean mBackPressed;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);
        initView();
        // handle arguments
        //mVideoPath = getIntent().getStringExtra("videoPath");
        //mVideoPath = "nfs://192.168.11.1/sdcard?/DCIM/20180211115845.mp4";
        Intent intent = getIntent();
        String json = intent.getStringExtra("json");
        Gson gson= new Gson();
        FileBean all = gson.fromJson(json, FileBean.class);
        mVideoPath = all.getCachePath();
        if(mVideoPath!=null&& NfsUtil.isImageFile(mVideoPath)){
            mVideoPath = null;
        }
        if(mVideoPath==null){
            mVideoPath = "nfs://"+ BaseStatic.DHCP_SERVICE_IP+"/sdcard?"+all.getNfsPath();
        }
        LogUtils.e("VideoActivity mVideoPath = "+mVideoPath);

        // init player
        IjkMediaPlayer.loadLibrariesOnce(null);
        IjkMediaPlayer.native_profileBegin("libijkplayer.so");

        myVideoView = findViewById(R.id.myVideoView);
        myVideoView.setOnCompletionListener(new IMediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(IMediaPlayer mp) {

            }

            @Override
            public void onError() {
                finish();
            }
        });
        myVideoView.setController(myController);
        if (mVideoPath != null)
        myVideoView.setVideoPath(mVideoPath);
        else {
            Log.e(TAG, "Null Data Source\n");
            finish();
            return;
        }
        myVideoView.start(1);
    }

    private void initView() {
        video_iv = findViewById(R.id.video_iv);
        myController = findViewById(R.id.my_controller);
        myController.setImageView(video_iv);
        video_iv.setOnClickListener(this);
    }

    @Override
    public void onBackPressed() {
        mBackPressed = true;
        super.onBackPressed();
    }

    @Override
    protected void onResume() {
        super.onResume();
        LogUtils.e("VideoActivity onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        LogUtils.e("VideoActivity onPause");

    }

    @Override
    protected void onStop() {
        super.onStop();
        if(mBackPressed){
            myVideoView.stopPlayback();
            myVideoView.release(true);
            myVideoView.stopBackgroundPlay();
        }
        myVideoView.pause();
        myVideoView.enterBackground();
        IjkMediaPlayer.native_profileEnd();
    }


    @Override
    public void onClick(View v) {
        if(v==video_iv){
            if(myController.isPlaying()){
                myController.pause();
                video_iv.setImageResource(R.mipmap.video_play);
            }else{
                myController.start();
                video_iv.setVisibility(View.GONE);
            }
        }
    }

}
